/**
 * 
 */
package info.pixstone.foodhunter.apps.service;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;

/**
 * @author MadStone
 * 
 */
public class FoodService extends Service implements Observer {

	/**
	 * 
	 */
	private static final String TAG = "FoodService";
	private static final Object sFoodLocationLock = new Object();
	private static FoodLocation foodLocation = null;
	private static final String UPDATE_LOCATION_ACTION = "info.pixstone.foodhunter.apps.UPDATE_LOCATION_ACTION";
	private static boolean intentSwitch = false;


	@Override
	public void onCreate() {
		Log.d(TAG, "onCreate");
		init();
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "onStartCommand");
		init();
		super.onStartCommand(intent, flags, startId);
		return START_STICKY;

	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy");
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public void update(Observable foodLocation, Object location) {
		Log.d(TAG, "Changed");
		Location locate = (Location) location;
		if (intentSwitch) {
			Intent intent = new Intent();
			intent.setAction(UPDATE_LOCATION_ACTION);
			intent.putExtra("Location",
					locate.getLatitude() + "," + locate.getLongitude());
			sendBroadcast(intent);
		}
	}

	private void init() {
		synchronized (sFoodLocationLock) {
			if (foodLocation == null) {
				foodLocation = new FoodLocation(getApplicationContext());
			}
			foodLocation.addObserver(this);

		}
	}

	public static void addLocationUpdate() {
		intentSwitch = true;
	}

	public static void removeLocationUpdate() {
		intentSwitch = false;
	}


	
	@SuppressWarnings("null")
	public static void getLocation(Context mContext) {

			Intent intent = new Intent();
			intent.setAction(UPDATE_LOCATION_ACTION);
			final List<Address> AddressList = null;
			intent.putExtra("Location", AddressList.get(0).toString());
			mContext.sendBroadcast(intent);
		
	}

}
